package com.bcntouch.touch2act.Suggest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.bcntouch.touch2act.R;
import com.bcntouch.touch2act.Opine.Opinion;
import com.bcntouch.touch2act.Opine.OpinionStorage;

public class NewSuggestionActivity extends Activity {

	private String mSuggestionType;
	private String mAnswer;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_suggestion);
//        getActionBar().setDisplayHomeAsUpEnabled(true);
        
        Intent intent = getIntent();
        String suggestionType = intent.getStringExtra(SuggestionTypesActivity.EXTRA_SUGGESTION_TYPE);
        if (suggestionType != null) {
        	TextView suggestionTypeTextView = (TextView) findViewById(R.id.suggestion_type_text_view);
        	suggestionTypeTextView.setText(suggestionType);
        	mSuggestionType = suggestionType;
        }
        
        Spinner spinner = (Spinner) findViewById(R.id.answer_spinner);
		// Create an ArrayAdapter using the string array and a default spinner
		// layout
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.si_no,
				android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		// Handler
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				mAnswer = (String)parent.getItemAtPosition(position);
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		// Apply the adapter to the spinner
		spinner.setAdapter(adapter);
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.activity_new_suggestion, menu);
//        return true;
//    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public void onSendButtonClick(View view) {
    	EditText suggestionEditText = (EditText) findViewById(R.id.suggestion_text);
    	String question = suggestionEditText.getText().toString();
    	
//    	ProgressDialog progressDialog = new ProgressDialog(this);
//    	progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//    	progressDialog.setMessage(getString(R.string.enviant_proposta));
//    	progressDialog.setCancelable(false);
//    	progressDialog.show();
//    	
//    	progressDialog.hide();
    	
    	int numPositiveVotes = 0;
    	int numNegativeVotes = 0;
    	int yourVote = 0;
    	if (getString(R.string.si).equals(mAnswer)) {
    		numPositiveVotes = 1;
    		yourVote = 1;
    	}
    	else {
    		numNegativeVotes = 1;
    		yourVote = 0;
    	}
    	
    	// Save the opinion in DB
    	Opinion opinion = new Opinion(null,
    								  question,
    								  mSuggestionType,
    								  1/*numVotes*/,
    								  numPositiveVotes,
    								  numNegativeVotes,
    								  1/*haveVoted*/,
    								  yourVote,
    								  1/*isYours*/);
    	
    	OpinionStorage.insertOpinion(this, opinion);
    	
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage(R.string.proposta_enviada)
    	       .setCancelable(false)
    	       .setPositiveButton(R.string.acceptar, new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   	Intent intent = new Intent(NewSuggestionActivity.this, SuggestActivity.class);
    	           		NavUtils.navigateUpTo(NewSuggestionActivity.this, intent);
    	           }
    	       });
    	AlertDialog alert = builder.create();
    	alert.show();
    }

}
